home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / clnwtmp.1 / clnwtmp / clnwtmp-1.0 / clnwtmp.c < prev    next >
C/C++ Source or Header  |  1995-12-25  |  4KB  |  167 lines

  1. /*    clnwtmp.c                                        Oct. 95
  2.     Mark Santelli
  3.  
  4.     Reads in records from the wtmp file and writes them out if the 
  5.     date from wtmp is greater than the date received in the command 
  6.     line. The time of '00:00:00' is used in the date spec. The 
  7.     wtmp or utmp filename is passed as the first parameter to the
  8.     program and the cutoff date is passed as the second. The new 
  9.     system file is created in the current directory.
  10. */
  11.  
  12. #include <ctype.h>
  13. #include <sys/types.h> 
  14. #include <sys/stat.h>
  15. #include <time.h>
  16. #include <utmp.h>
  17. #include <fcntl.h>
  18.  
  19. #define BLOCK    140
  20.  
  21. struct utmp filebuf[BLOCK];
  22. struct utmp outbuf [BLOCK];
  23.  
  24. time_t time_threshold;
  25. int infile_handle, outfile_handle;
  26.  
  27. int  OpenFiles(char *);
  28. int  ValidInput(int, char * []);
  29. void ConvertDate(int, int, int);
  30.  
  31. void main(int argcnt, char *args[])
  32. {
  33.     int bytes, sub, outsub = 0;
  34.                                 /* validate input */
  35.     if (!ValidInput(argcnt, args))
  36.         exit(1);
  37.                             
  38.     if (!OpenFiles(args[1]))
  39.         exit(1);
  40.  
  41.     while ((bytes = read(infile_handle, &filebuf, sizeof(filebuf))) > 0)
  42.     {    
  43.         bytes /= sizeof(struct utmp);
  44.         for (sub = 0; sub < bytes; sub++) 
  45.         {
  46.             if (filebuf[sub].ut_time > time_threshold)
  47.                 outbuf[outsub++] = filebuf[sub];
  48.         }
  49.  
  50.         if (write(outfile_handle, &outbuf, sizeof(struct utmp) * outsub) 
  51.             != sizeof(struct utmp) * outsub)
  52.         {
  53.             perror("write error.");
  54.             exit(1);
  55.         }
  56.  
  57.         outsub = 0;
  58.     }
  59.  
  60.     exit(0);             
  61.  
  62.  
  63. int ValidInput(int parmcnt, char * parms[])
  64. {
  65.     int day, mon, yr, check;
  66.     struct stat st;
  67.                             
  68.     if (parmcnt < 3)                /* correct # of parms */
  69.     {
  70.         printf("\nusage:  %s   [/path/wtmp YYMMDD]\t  where:\n", parms[0]); 
  71.         printf("\n- '/path/wtmp' is the pathname of the system login file\n");
  72.         printf("- YYMMDD is the cutoff date\n");
  73.         printf("\tYY must be in the range 01-99\n");           
  74.         printf("\tMM must be in the range 01-12\n");
  75.         printf("\tDD must be in the range 01-31\n");
  76.         printf("\nCreates a new system file not containing the records whose\n");
  77.         printf("dates are less than the date passed on the command line.\n");
  78.         printf("'utmp' can also be rewritten.\n");
  79.         return 0;
  80.     }
  81.                                 /* does file exists */
  82.     if (stat(parms[1], &st) == -1)
  83.     {
  84.         perror("\nstat error. ");
  85.         return 0;
  86.     }
  87.                                 /* date validation */
  88.     if (strlen(parms[2]) == 6) 
  89.     {                            /* is date numeric */
  90.         for (check = 0; check < 6; check++)
  91.         {
  92.             if (parms[2][check] < '0' || parms[2][check] > '9')
  93.             {
  94.                 printf("\ndate contains non-numeric data\n");
  95.                 return 0; 
  96.             }        
  97.         }    
  98.                                 /* test ranges */
  99.         day = atoi(&parms[2][4]);
  100.         parms[2][4] = '\0';        
  101.          if (day < 1 || day > 31)
  102.         {
  103.             printf("\nday must be an integer from (01 <--> 31)\n");
  104.             return 0;
  105.         }
  106.         mon = atoi(&parms[2][2]) - 1;
  107.         parms[2][2] = '\0';        
  108.          if (mon < 0 || mon > 11)
  109.         {
  110.             printf("\nmonth must be an integer from (01 <--> 12)\n");
  111.             return 0;
  112.         }
  113.         yr = atoi(parms[2]);
  114.         if (yr < 1 || yr > 99)
  115.         {
  116.             printf("\nyear must be an integer from (01 <--> 99)\n");
  117.             return 0;
  118.         }
  119.                                 /* convert date received into      */
  120.          ConvertDate(day, mon, yr);    /* a time_t format like in wtmp */
  121.      }                                 
  122.     else
  123.     {
  124.         printf("\ndate must be in YYMMDD format\n");
  125.         return 0;
  126.     }
  127.  
  128.     return 1;
  129. }
  130.  
  131.  
  132. void ConvertDate(int d, int m, int y)
  133. {
  134.     struct tm cutdate;
  135.       
  136.     cutdate.tm_sec   = 0;
  137.     cutdate.tm_min   = 0;
  138.     cutdate.tm_hour  = 0;
  139.     cutdate.tm_mday  = d;
  140.     cutdate.tm_mon      = m;
  141.     cutdate.tm_year  = y;
  142.     cutdate.tm_isdst = -1; 
  143.  
  144.     time_threshold = mktime(&cutdate);
  145. }
  146.                     
  147. int OpenFiles(char * path)
  148. {
  149.     if ((infile_handle = open(path, O_RDONLY )) == -1)
  150.     {
  151.         perror("\nerror on open 'wtmp'\n");
  152.         return 0;
  153.     }
  154.     
  155.     if ((outfile_handle = open("new.wtmp", O_WRONLY | O_CREAT | 
  156.                                     O_TRUNC, S_IWRITE)) == -1)
  157.     {
  158.         perror("\nerror on open 'new.wtmp'\n");
  159.         return 0;
  160.     }
  161.  
  162.     return 1;
  163. }
  164.  
  165.  
  166.